package com.huluobo.a2204monthdemo.register

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.a2204monthdemo.R
import com.huluobo.a2204monthdemo.databinding.ActivityRegisterBinding
import com.huluobo.base.view.BaseMVVMActivity

class RegisterActivity : BaseMVVMActivity<ActivityRegisterBinding, RegisterViewModel>() {
    override fun initData() {
        viewModel.registerSuccess.observe(this) {
            ToastUtils.showLong(it.message)
            if (it.code == 200) {
                finish()
            }
        }

        viewModel.registerFailed.observe(this) {
            Log.i("TagA", "注册失败:$it")
        }
    }

    override fun initView() {
        dataBinding.registerBtn.setOnClickListener {
            val usernameString = dataBinding.registerUsername.text.toString()
            val passwordString = dataBinding.registerPassword.text.toString()
            val rePasswordString = dataBinding.registerRePassword.text.toString()

            if (usernameString.isNotEmpty() && passwordString.isNotEmpty() && rePasswordString.isNotEmpty()) {
                if (TextUtils.equals(passwordString, rePasswordString)) {
                    viewModel.register(usernameString, passwordString, rePasswordString)
                } else {
                    dataBinding.registerRePassword.error = "两次密码输入不一致"
                }
            } else {
                dataBinding.registerUsername.error = "用户名密码再次输入密码不能为空"
            }
        }
    }

    override fun getViewModel(): Class<RegisterViewModel> {
        return RegisterViewModel::class.java
    }

    override fun getLayoutId(): Int = R.layout.activity_register
}